home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 115_01.zip / ED2.C < prev    next >
Text File  |  1993-06-01  |  9KB  |  500 lines

  1. /* Screen editor:  main program
  2.  *
  3.  * Source:  ed2.c
  4.  * Version: September 5, 1981.
  5.  */
  6.  
  7. /* define signon message */
  8.  
  9. #define SIGNON "small-c editor, version 2:  December 20, 1981."
  10.  
  11. /* the main program dispatches the routines that
  12.  * handle the various modes.
  13.  */
  14.  
  15. #define CMNDMODE 1    /* enter command mode flag */
  16. #define INSMODE  2    /* enter insert modes flag */
  17. #define EDITMODE 3    /* enter edit mode flag */
  18. #define EXITMODE 4    /* exit editor flag */
  19.  
  20. main()
  21. {
  22. int mode;
  23.     /* fmt output by default goes to screen */
  24.     fmtassn(NO);
  25.     /* set tabs, clear the screen and sign on */
  26.     fmtset(8);
  27.     outclr();
  28.     outxy(0,SCRNL1);
  29.     message(SIGNON);
  30.     outxy(0,1);
  31.     /* clear filename[] for save(), resave() */
  32.     name("");
  33.     /* clear the main buffer */
  34.     bufnew();
  35.     /* start off in command mode */
  36.     mode=CMNDMODE;
  37.     /* get null line 1 for edit() */
  38.     edgetln();
  39.     while(1){
  40.         if (mode==EXITMODE) {
  41.             break;
  42.         }
  43.         else if (mode==CMNDMODE) {
  44.             mode=command();
  45.         }
  46.         else if (mode==EDITMODE) {
  47.             mode=edit();
  48.         }
  49.         else if (mode==INSMODE) {
  50.             mode=insert();
  51.         }
  52.         else {
  53.             syserr("main: no mode");
  54.             mode=EDITMODE;
  55.         }
  56.     }
  57. }
  58.  
  59. /*
  60.  * handle edit mode.
  61.  * dispatch the proper routine based on one-character commands.
  62.  */
  63.  
  64. edit()
  65. {
  66. char buffer[SCRNW1];
  67. int v;
  68. int x,y;
  69. char c;
  70.     /* we can't do edgetln() or edgo() here because
  71.      * those calls reset the cursor.
  72.      */
  73.     pmtedit();
  74.     while(1){
  75.         /* get command */
  76.         c=tolower(syscin());
  77.         if ((c==ESC1)|(c=='c')) {
  78.             /* enter command mode. */
  79.             return(CMNDMODE);
  80.         }
  81.         else if ((c==INS1)|(c=='i')) {
  82.             /* enter insert mode */
  83.             return(INSMODE);
  84.         }
  85.         else if (special(c) == YES) {
  86.             if ((c == UP1)|(c == DOWN1)) {
  87.                 return(INSMODE);
  88.             }
  89.             else {
  90.                 continue;
  91.             }
  92.         }
  93.         else if (control(c)==YES) {
  94.             continue;
  95.         }
  96.         else if (c==' ') {
  97.             edright();
  98.             pmtcol();
  99.         }
  100.         else if (c=='b') {
  101.             edbegin();
  102.             pmtcol();
  103.         }
  104.         else if (c=='d') {
  105.             /* scroll down */
  106.             pmtmode("edit: scroll");
  107.             while (bufnrbot()==NO) {
  108.                 if (chkkey()==YES) {
  109.                     break;
  110.                 }
  111.                 if (eddn()==ERR) {
  112.                     break;
  113.                 }
  114.             }
  115.             pmtedit();
  116.         }
  117.         else if (c=='e') {
  118.             edend();
  119.             pmtcol();
  120.         }
  121.         else if (c=='g') {
  122.             /* save x,y in case don't get number */
  123.             x=outgetx();
  124.             y=outgety();
  125.             pmtcmnd("edit: goto: ",buffer);
  126.             if(number(buffer,&v)) {
  127.                 edgo(v,0);
  128.             }
  129.             else {
  130.                 outxy(x,y);
  131.             }
  132.             pmtedit();
  133.         }
  134.         else if (c=='k') {
  135.             pmtmode("edit: kill");
  136.             c=syscin();
  137.             if ((special(c)==NO) &
  138.                 (control(c)==NO)) {
  139.                 edkill(c);
  140.             }
  141.             pmtedit();
  142.         }
  143.         else if (c=='s') {
  144.             pmtmode("edit: search");
  145.             c=syscin();
  146.             if ((special(c)==NO) &
  147.                 (control(c)==NO)) {
  148.                 edsrch(c);
  149.             }
  150.             pmtedit();
  151.         }
  152.         else if (c=='u') {
  153.             /* scroll up */
  154.             pmtmode("edit: scroll");
  155.             while (bufattop()==NO) {
  156.                 if (chkkey()==YES) {
  157.                     break;
  158.                 }
  159.                 if (edup()==ERR) {
  160.                     break;
  161.                 }
  162.             }
  163.             pmtedit();
  164.         }
  165.         else if (c=='x') {
  166.             pmtmode("edit: eXchange");
  167.             c=syscin();
  168.             if ((special(c)==NO) &
  169.                 (control(c)==NO)) {
  170.                 edchng(c);
  171.             }
  172.             pmtedit();
  173.         }
  174.         /* do nothing if command not found */
  175.     }
  176. }
  177.  
  178. /* insert mode.
  179.  * in this mode the UP1, UP2 keys reverse their roles,
  180.  * as do the DOWN1, and DOWN2 keys.
  181.  */
  182.  
  183. insert()
  184. {
  185. char c;
  186.     pmtmode("insert");
  187.     while (1) {
  188.         /* get command */
  189.         c=syscin();
  190.         if (c==ESC1) {
  191.             /* enter command mode */
  192.             return(CMNDMODE);
  193.         }
  194.         else if (c==EDIT1) {
  195.             /* enter edit mode */
  196.             return(EDITMODE);
  197.         }
  198.         else if (c==INS1) {
  199.             /* do nothing */
  200.             ;
  201.         }
  202.         else if (special(c)==YES) {
  203.             if ((c == UP2)|(c == DOWN2)) {
  204.                 return(EDITMODE);
  205.             }
  206.             else {
  207.                 continue;
  208.             }
  209.         }
  210.         else if (control(c)==YES) {
  211.             /* ignore non-special control chars */
  212.             continue;
  213.         }
  214.         else {
  215.             /* insert one char in line */
  216.             edins(c);
  217.             pmtcol();
  218.         }
  219.     }
  220. }
  221.  
  222. /* return YES if c is a control char */
  223.  
  224. control(c) char c;
  225. {
  226.     if (c==TAB) {
  227.         return(NO);    /* tab is regular */
  228.     }
  229.     else if (c>=127) {
  230.         return(YES);    /* del or high bit on */
  231.     }
  232.     else if (c<32) {
  233.         return(YES);    /* control char */
  234.     }
  235.     else {
  236.         return(NO);    /* normal */
  237.     }
  238. }
  239.  
  240. /*
  241.  * handle the default actions of all special keys.
  242.  * return YES if c is one of the keys.
  243.  */
  244.  
  245. special(c) char c;
  246. {
  247. int k;
  248.     if (c==JOIN1) {
  249.         edjoin();
  250.         pmtline();
  251.         return(YES);
  252.     }
  253.     if (c==SPLT1) {
  254.         edsplit();
  255.         pmtline();
  256.         return(YES);
  257.     }
  258.     if (c==ABT1) {
  259.         edabt();
  260.         pmtcol();
  261.         return(YES);
  262.     }
  263.     else if (c==DEL1) {
  264.         eddel();
  265.         pmtcol();
  266.         return(YES);
  267.     }
  268.     else if (c==ZAP1) {
  269.         edzap();
  270.         pmtline();
  271.         return(YES);
  272.     }
  273.     else if (c==UP2) {
  274.         /* move up */
  275.         edup();
  276.         pmtline();
  277.         return(YES);
  278.     }
  279.     else if (c==UP1) {
  280.         /* insert up */
  281.         ednewup();
  282.         pmtline();
  283.         return(YES);
  284.     }
  285.     else if (c==DOWN2) {
  286.         /* move down */
  287.         eddn();
  288.         pmtline();
  289.         return(YES);
  290.     }
  291.     else if (c==DOWN1) {
  292.         /* insert down */
  293.         ednewdn();
  294.         pmtline();
  295.         return(YES);
  296.     }
  297.     else if (c==LEFT1) {
  298.         edleft();
  299.         pmtcol();
  300.         return(YES);
  301.     }
  302.     else if (c==RIGHT1) {
  303.         edright();
  304.         pmtcol();
  305.         return(YES);
  306.     }
  307.     else {
  308.         return(NO);
  309.     }
  310. }
  311.  
  312. /*
  313.  * command() dispatches command routines while
  314.  * in command mode.
  315.  */
  316.  
  317. command()
  318. {
  319. int v;
  320. char c;
  321. char args[SCRNW1];
  322. char *argp;
  323. int topline;
  324. int ypos;
  325. int oldline;
  326. int k;
  327.     /* command mode commands may move the current line.
  328.      * command mode must save the current line on entry
  329.      * and restore it on exit.
  330.      */
  331.     edrepl();
  332.     /* remember how the screen was drawn on entry */
  333.     oldline=bufln();
  334.     ypos=outgety();
  335.     topline=oldline-ypos+1;
  336.     while(1) {
  337.         outxy(0,SCRNL1);
  338.         fmtcrlf();
  339.         pmtmode("command:");
  340.         getcmnd(args,0);
  341.         fmtcrlf();
  342.         pmtline();
  343.         c=args[0];
  344.         if ((c==EDIT1)|(c==INS1)) {
  345.             /* redraw screen */
  346.             if (oldline==bufln()) {
  347.                 /* get current line */
  348.                 edgetln();
  349.                 /* redraw old screen */
  350.                 bufout(topline,1,SCRNL1);
  351.                 outxy(0,ypos);
  352.             }
  353.             else {
  354.                 /* update line and screen */
  355.                 edgo(bufln(),0);
  356.             }
  357.             if (c==EDIT1) {
  358.                 return (EDITMODE);
  359.             }
  360.             else {
  361.                 return (INSMODE);
  362.             }
  363.         }
  364.         else if (tolower(args[0])=='g'){
  365.             argp=skipbl(args+1);
  366.             if (argp[0]==EOS) {
  367.                 edgo(oldline,0); 
  368.                 return(EDITMODE);
  369.             }
  370.             else if (number(argp,&v)==YES) {
  371.                 edgo(v,0);
  372.                 return(EDITMODE);
  373.             }
  374.             else {
  375.                 message("bad line number");
  376.             }
  377.         }
  378.         else if (lookup(args,"append")) {
  379.             append(args);
  380.         }
  381.         else if (lookup(args,"change")) {
  382.             change(args);
  383.         }
  384.         else if (lookup(args,"clear")) {
  385.             clear();
  386.         }
  387.         else if (lookup(args,"delete")) {
  388.             delete(args);
  389.         }
  390.         else if (lookup(args,"dos")) {
  391.             if (chkbuf()==YES) {
  392.                 return (EXITMODE);
  393.             }
  394.         }
  395.         else if (lookup(args,"find")) {
  396.             if ((k = find()) >= 0) {
  397.                 edgo(bufln(),k);
  398.                 return(EDITMODE);
  399.             }
  400.             else {
  401.                 /* get current line */
  402.                 bufgo(oldline);
  403.                 edgetln();
  404.                 /* stay in command mode */
  405.                 message("pattern not found");
  406.             }
  407.         }
  408.         else if (lookup(args,"list")) {
  409.             list(args);
  410.         }
  411.         else if (lookup(args,"load")) {
  412.             load(args);
  413.         }
  414.         else if (lookup(args,"name")) {
  415.             name(args);
  416.         }
  417.         else if (lookup(args,"resave")) {
  418.             resave();
  419.         }
  420.         else if (lookup(args,"save")) {
  421.             save();
  422.         }
  423.         else if (lookup(args,"search")) {
  424.             search(args);
  425.         }
  426.         else if (lookup(args,"tabs")) {
  427.             tabs(args);
  428.         }
  429.         else {
  430.             message("command not found");
  431.         }
  432.     }
  433. }
  434.  
  435. /* return YES if line starts with command */
  436.  
  437. lookup(line,command) char *line, *command;
  438. {
  439.     while(*command) {
  440.         if (tolower(*line++)!=*command++) {
  441.             return(NO);
  442.         }
  443.     }
  444.     if((*line==EOS)|(*line==' ')|(*line==TAB)) {
  445.         return(YES);
  446.     }
  447.     else {
  448.         return(NO);
  449.     }
  450. }
  451.  
  452. /* get next command into argument buffer */
  453.  
  454. getcmnd(args,offset) char *args; int offset;
  455. {
  456. int j,k;
  457. char c;
  458.     outxy(offset,outgety());
  459.     outdeol();
  460.     k=0;
  461.     while ((c=syscin())!=CR) {
  462.         if ((c==EDIT1)|(c==INS1)) {
  463.             args[0]=c;
  464.             return;
  465.         }
  466.         if ((c==DEL1)|(c==LEFT1)) {
  467.             if (k>0) {
  468.                 outxy(offset,outgety());
  469.                 outdeol();
  470.                 k--;
  471.                 j=0;
  472.                 while (j<k) {
  473.                     outchar(args[j++]);
  474.                 }
  475.             }
  476.         }
  477.         else if (c==ABT1) {
  478.             outxy(offset,outgety());
  479.             outdeol();
  480.             k=0;
  481.         }
  482.         else if ((c!=TAB)&((c<32)|(c==127))) {
  483.             /* do nothing */
  484.             continue;
  485.         }
  486.         else {
  487.             if ((k+offset)<SCRNW1) {
  488.                 args[k++]=c;
  489.                 outchar(c);
  490.             }
  491.         }
  492.     }
  493.     args[k]=EOS;
  494. }
  495. message("command not found");
  496.         }
  497.     }
  498. }
  499.  
  500. /* return YES if line starts with